package com.friend.campusnotice.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.friend.campusnotice.R;
import com.friend.campusnotice.bean.Contacts;

import java.util.List;

/**
 * Created by Administrator on 2016/5/24.
 */
public class ContactListAdapter extends RecyclerView.Adapter<ContactListAdapter.ContactHolder> {
    private Context mContext;
    private List<Contacts> contactsList;
    private onItemClickListener onItemClickListener;

    public ContactListAdapter(Context mContext, List<Contacts> contactsList) {
        this.mContext = mContext;
        this.contactsList = contactsList;
    }

    public void setOnItemClickListener(ContactListAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }



    @Override
    public ContactHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ContactHolder holder = new ContactHolder(LayoutInflater.from(mContext)
                .inflate(R.layout.contact_item_layout, parent,false));
        return holder;

    }

    @Override
    public void onBindViewHolder(final ContactHolder holder, int position) {

        holder.contactName.setText(contactsList.get(position).getContactName());
        holder.contactFrist.setText(contactsList.get(position).getContactName().substring(contactsList.get(position).getContactName().length()-1));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, pos);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return contactsList.size();
    }

    class ContactHolder extends RecyclerView.ViewHolder{
        TextView contactName;
        Button contactFrist;


        public ContactHolder(View view) {
            super(view);
            contactName = (TextView) view.findViewById(R.id.contact_real_name);
            contactFrist = (Button) view.findViewById(R.id.contact_name);
        }
    }

    public  interface onItemClickListener{
        void onItemClick(View view, int position);
    }

}
